<?php
	if (!class_exists('Connection')) {
		require_once '../mdl/Connection.php';
	}
	class SelectUser extends Connection {
		private $conn;

		public function __construct() {
			Connection::init();
			$this->conn = $this->getConnection();
		}

		public function checkMail($email) {
			$stmt = $this->conn->prepare("	select count(id)
										from user
										where email =  ?");
			$stmt->bindValue(1, $email);
			$stmt->execute();

			return $stmt->fetchColumn();
		}

		public function checkLogin($email, $pass) {
			$stmt = $this->conn->prepare("	select count(id)
										from user
										where email =  ? and password = ?");
			$stmt->bindValue(1, $email);
			$stmt->bindValue(2, hash("sha512", $pass));
			$stmt->execute();

			return $stmt->fetchColumn();
		}

		public function extractUser($email, $pass) {
			if ($this->checkLogin($_POST["username"], $_POST["password"])) {
				//extract from User Table
				$stmt = $this->conn->prepare("	select *
											from user
											where email =  ? and password = ?");
				$stmt->bindValue(1, $email);
				$stmt->bindValue(2, hash("sha512", $pass));
				$stmt->execute();
				$userObjs = $stmt->fetchAll(PDO::FETCH_ASSOC);

				//extract Privileges name
				$userPrivs = new SelectUserPrivileges();
				$userObjs[0]["privileges"] = $userPrivs->extractPrivileges((int)$userObjs[0]["privileges_id"]);
				if (isset($userObjs)) unset($userObjs[0]["privileges_id"]);
				//extract Status

				$userStatus = new SelectUserStatus();
				$userObjs[0]["status"] = $userStatus->extractStatus((int)$userObjs[0]["status_id"]);
				unset($userObjs[0]["status_id"]);
				$obj                   = new User($userObjs[0]);

				return $obj;
			}
		}

		public function extractAllUserData(User $obj) {
			$companiesArrayObj = array();
			$promosArrayObj = array();
			$shopsArrayObj = array();

			// extract companies id for this user
			$CompanyUser= new SelectCompanyUser();
			$userCompanies = $CompanyUser->extractCompanyId((int)$obj->getId());


			// extract companies info

			foreach ($userCompanies as $val) {

				$company = new SelectCompany();

				$companies = $company->extractCompany((int)$val["company_id"]);


				//extract all shops for this company
				$shopExtr = new SelectShop();
				$shops = $shopExtr->extractShop((int)$companies[0]["id"]);

				foreach ($shops as $shop) {

					//extract delivery status for this shop
					if ($shop["delivery_id"] != NULL) {
						$delivery = new SelectDelivery();
						$shop["delivery"] =$delivery->extractDelivery((int)$shop["delivery_id"]);

					} else {
						$shop["delivery"] = "Неопределена";
					}




					//extract promotions for this shop
					$shopExtr = new SelectShopPromos();
					$Promos =$shopExtr->extractShopPromos((int)$shop["id"]);

					foreach ($Promos as $promo){
						$promosArrayObj[] = new ShopPromo($promo);

					}
					//create array of objects
					$shop["shop_promo"]=$promosArrayObj;

					$shopsArrayObj[] = new Shop($shop);

				}
				//create array of objects
				$companies[0]["shops"]= $shopsArrayObj;
				$companiesArrayObj[] = new Company($companies[0]);
				$obj->setCompanies($companiesArrayObj);
				unset($shopsArrayObj);
			};
		}
		public function countNotApprovedUsers(){
			$stmt = $this->conn->query("	SELECT COUNT( u.id )
											FROM  `user` u
											JOIN user_status s ON ( u.status_id = s.id )
											WHERE s.name =  'not approved'");

			$stmt->execute();
			$count = (int)$stmt->fetchColumn();;

			return $count;
		}
		public function showNotApprovedUsers(){
			$stmt = $this->conn->query("	select *
											from user
											where status_id = 1 ");

			$stmt->execute();
			$userObjs = $stmt->fetchAll(PDO::FETCH_ASSOC);


			foreach ($userObjs as $uobj){

				//extract Privileges name
				$userPrivs = new SelectUserPrivileges();
				$uobj["privileges"] = $userPrivs->extractPrivileges((int)$uobj["privileges_id"]);
				unset($uobj["privileges_id"]);
				//extract Status

				$userStatus = new SelectUserStatus();
				$uobj["status"] = $userStatus->extractStatus((int)$uobj["status_id"]);
				unset($uobj["status_id"]);
				$obj[]                   = new User($uobj);
			}


			return $obj;
		}
	}


?>